package com.yyh1;

import java.util.Objects;
import java.util.Scanner;

/*七彩鲜花管理系统
* 用接口来实现
* 1.定义鲜花类Flower，放入实体层
2.定义鲜花管理类FlowerMgr，通过一个数组实现对鲜花的管理，放入业务层。
3.FlowerMgr类中，实现public void inital(){}方法，用于完成对鲜花管理系统的初始化功能（数组的初始化）
4.FlowerMgr类中，实现public void mainMenu(){}方法，用于完成功能面板的展示及功能选择
5.FlowerMgr类中，实现private void searchAll(){}方法，用于完成查询订单的功能
6.FlowerMgr类中，实现private void updateSale(){}方法，用于完成修改订单的功能

7.FlowerMgr类中，实现private void deleteSale(){}方法，用于完成删除订单的功能
8.定义测试类Test，放入表示层；在测试类中，先初始化鲜花管理系统，然后调用mainMenu()方法,实现具体功能
9.编写代码过程中，尽量每完成一个功能，就对其进行测试*/
public class Test1 {
    public static void main(String[] args) {
        FlowerMgr flowerMgr = new FlowerMgrImpl();
        flowerMgr.inital();
        flowerMgr.mainMenu();
    }
}
//鲜花类Flower
class Flower {
    /*
   这是一个鲜花订单的事物描述类
 */

    // 属性
    private String id;
    private String name;
    private int number;
    private double price;
    private String date;
    private String salEmp;
    private String beiZhu;
    //无参构造方法
    public Flower() {
    }
    //有参构造方法
    public Flower(String id, String name, int number, double price, String date, String salEmp, String beiZhu) {
        this.id = id;
        this.name = name;
        this.number = number;
        this.price = price;
        this.date = date;
        this.salEmp = salEmp;
        this.beiZhu = beiZhu;
    }
    //getter和setter方法
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getSalEmp() {
        return salEmp;
    }

    public void setSalEmp(String salEmp) {
        this.salEmp = salEmp;
    }

    public String getBeiZhu() {
        return beiZhu;
    }

    public void setBeiZhu(String beiZhu) {
        this.beiZhu = beiZhu;
    }
    //重写equals()和hashCode()方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Flower flower = (Flower) o;
        return number == flower.number && Double.compare(flower.price, price) == 0 && Objects.equals(id, flower.id) && Objects.equals(name, flower.name) && Objects.equals(date, flower.date) && Objects.equals(salEmp, flower.salEmp) && Objects.equals(beiZhu, flower.beiZhu);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, number, price, date, salEmp, beiZhu);
    }
    //toString()方法
    @Override
    public String toString() {
        return "Flower{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", number=" + number +
                ", price=" + price +
                ", date='" + date + '\'' +
                ", salEmp='" + salEmp + '\'' +
                ", beiZhu='" + beiZhu + '\'' +
                '}';
    }
}
//鲜花管理类FlowerMgr
interface FlowerMgr {
    //初始化
    void inital();
    //主菜单
    void mainMenu();
    //查询所有
    void searchAll();
    //修改
    void updateSale();
    //删除
    void deleteSale();
}
//鲜花管理类FlowerMgr的实现类FlowerMgrImpl
class FlowerMgrImpl implements FlowerMgr {
    //鲜花数组
    private Flower[] flowers;
    //当前鲜花数量
    private int size;
    //键盘录入
    private Scanner scanner;
    // 辅助方法：根据ID查找鲜花
    private Flower getIndex(String id) {
        for (int i = 0; i < size; i++) {
            if (flowers[i].getId().equals(id)) {
                return flowers[i];
            }
        }
        return null;
    }
    @Override
    public void inital() {
        flowers = new Flower[5];
        size = 5;
        scanner = new Scanner(System.in);
        flowers[0] = new Flower("1", "向日葵", 10, 10.0, "2020-01-01", "张三", "无");
        flowers[1] = new Flower("2", "玫瑰", 20, 20.0, "2020-01-01", "张三", "无");
        flowers[2] = new Flower("3", "百合", 30, 30.0, "2020-01-01", "张三", "无");
        flowers[3] = new Flower("4", "康乃馨", 40, 40.0, "2020-01-01", "张三", "无");
        flowers[4] = new Flower("5", "郁金香", 50, 50.0, "2020-01-01", "张三", "无");
        System.out.println("系统初始化完成！");

    }
    @Override
    public void mainMenu() {
        while (true) {
            System.out.println("====================欢迎光临\"七彩鲜花\"销售管理系统========================");
            System.out.println("1.查询销售订单");
            System.out.println("2.修改销售订单");
            System.out.println("3.删除销售订单");
            System.out.println("4.系统退出");
            System.out.println("=========================================================================");
            System.out.print("请输入您的选择：");
            int choice = scanner.nextInt();
            scanner.nextLine();//消耗换行符
            //处理选择
            switch (choice) {
                case 1:
                    searchAll();
                    break;
                case 2:
                    updateSale();
                    break;
                case 3:
                    deleteSale();
                    break;
                case 4:
                    System.out.println("感谢使用\"七彩鲜花\"销售管理系统！");
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入错误，请重新输入1-4的数字！");
            }
        }
    }
    @Override
    public void searchAll() {
        System.out.println("查询所有销售订单：");
        if (size == 0){
            System.out.println("暂无数据！");
            return;
        }
        for (int i = 0; i < size; i++) {
            System.out.println(flowers[i]);
        }
        System.out.println("查询完毕！");
        System.out.println("按回车键继续...");
    }
    @Override
    public void updateSale() {
        System.out.println("修改销售订单：");
        searchAll();
        System.out.println("请输入要修改的订单编号：");
        String id = scanner.nextLine();
        Flower flower = getIndex(id);
        if (flower == null){
            System.out.println("未找到该订单！");
            return;
        }
        System.out.println("当前订单信息："+ flower);
        try {
            System.out.print("请输入新的鲜花名称(直接回车保留原值 '" + flower.getName() + "'): ");
            String name = scanner.nextLine();
            if (!name.isEmpty()) flower.setName(name);

            System.out.print("请输入新的数量(直接回车保留原值 " + flower.getNumber() + "): ");
            String numStr = scanner.nextLine();
            if (!numStr.isEmpty()) flower.setNumber(Integer.parseInt(numStr));

            System.out.print("请输入新的单价(直接回车保留原值 " + flower.getPrice() + "): ");
            String priceStr = scanner.nextLine();
            if (!priceStr.isEmpty()) flower.setPrice(Double.parseDouble(priceStr));

            System.out.print("请输入新的日期(直接回车保留原值 '" + flower.getDate() + "'): ");
            String date = scanner.nextLine();
            if (!date.isEmpty()) flower.setDate(date);

            System.out.print("请输入新的销售员(直接回车保留原值 '" + flower.getSalEmp() + "'): ");
            String salEmp = scanner.nextLine();
            if (!salEmp.isEmpty()) flower.setSalEmp(salEmp);

            System.out.print("请输入新的备注(直接回车保留原值 '" + flower.getBeiZhu() + "'): ");
            String beiZhu = scanner.nextLine();
            if (!beiZhu.isEmpty()) flower.setBeiZhu(beiZhu);

            System.out.println("订单修改成功！");
            System.out.println("修改后信息: " + flower);
        } catch (NumberFormatException e) {
            System.out.println("输入格式错误，修改失败！");
        }
    }
    @Override
    public void deleteSale() {
        System.out.println("删除销售订单：");
        searchAll();
        System.out.println("请输入要删除的订单编号：");
        String id = scanner.next();
        int index = -1;
        for (int i = 0; i < size; i++) {
            if (flowers[i].getId().equals(id)) {
                index = i;
                break;
            }
        }
        if (index == -1) {
            System.out.println("未找到该订单！");
            return;
        }
        //删除
        for (int i = index; i < size - 1; i++) {
            flowers[i] = flowers[i + 1];
        }
        size--;
        System.out.println("删除成功！");
    }
}
